/*
   Copyright (c) [2023] [魔凤啸天]
   [MFUI] is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details.  
*/
#include "textbutton.hpp"

namespace MFUI{
	TextButton::~TextButton(){}
	TextButton::TextButton(const std::string& text,Font* font,int fontsize,
	SDL_Color textcolor,SDL_Color background):
	TextNodeBase(text,font,fontsize,textcolor,background,align_m){}
	void TextButton::_updatebase(){redraw=true;notifyparent();}
	void TextButton::_updatetext(){_updatebase();}
	void TextButton::_pressed(Window* w,SDL_FPoint p){_updatebase();pressed(w,p);}
	void TextButton::_released(Window* w,SDL_FPoint p){_updatebase();released(w,p);}
	void TextButton::draw(Renderer* r,bool updatecache){
		_draw(r,updatecache);
		auto back=background.a,a=textcolor.a;
		switch(getstate()){
		  case state_released:break;
		  case state_on:back>>=1,a>>=1;break;
		  case state_pressed:back>>=2,a>>=2;break;
		}
		auto cache=background.a,alpha=textcolor.a;
		background.a=back;
		textcolor.a=a;
		_tryrendertext(r,&rect);
		textcolor.a=alpha;
		background.a=cache;
		redraw=false;
		makecircle(r,rect);
	}
	void TextButton::mouseentered(Window* w){_updatebase();}
	void TextButton::mouseexited(Window* w){_updatebase();}
	void TextButton::_losefocus(Window* w){_updatebase();}
}